package com.example.rfidwms;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class recy_item_classify_Adapter extends RecyclerView.Adapter<recy_item_classify_Adapter.ViewHolder> {
    public List<Map<String,Object>> list=new ArrayList<>();
    private OnItemClickListener onItemClickListener;

    public Context con;
    public LayoutInflater inflater;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener= onItemClickListener;
    }

    public interface OnItemClickListener{
        void onItemClick(View view, int position);
//        多选
//        void onItemLongClick(View view,int position);
    }

    public recy_item_classify_Adapter(List<Map<String,Object>> list, Context con){
        this.con=con;
        this.list=list;
        inflater=LayoutInflater.from(con);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= inflater.inflate(R.layout.recyclerview_item,null);
        ViewHolder viewHolder=new ViewHolder(view);
        return viewHolder;

    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        Set<Integer> positionSet = WriteTagActivity.positionSet;
        //检查set里是否包含position，包含则显示选中的背景色，不包含则反之
        if (positionSet.contains(position)) {
            holder.itemView.setBackgroundColor(ContextCompat.getColor(con,R.color.purple_200));
//            传递要写入的id
//            Toast.makeText(con.getApplicationContext(), list.get(position).get("id").toString(), Toast.LENGTH_SHORT).show();

        } else {
            holder.itemView.setBackgroundColor(ContextCompat.getColor(con,R.color.white));
        }
        holder.id.setText(list.get(position).get("id").toString());
        holder.classify_num.setText(list.get(position).get("classify_num").toString());
        holder.name.setText(list.get(position).get("name").toString());
        holder.spec.setText(list.get(position).get("specs").toString());
        holder.unit.setText(list.get(position).get("unit").toString());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(v,position);
            }
        });
//        多选
//        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                onItemClickListener.onItemLongClick(v,position);
//                return false;
//            }
//        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        public TextView id;
        public TextView classify_num;
        public TextView name;
        public TextView spec;
        public TextView unit;

        public ViewHolder(View itemView) {
            super(itemView);
            id=(TextView) itemView.findViewById(R.id.recy_M_id);
            classify_num= (TextView) itemView.findViewById(R.id.recy_M_classify_num);
            name= (TextView) itemView.findViewById(R.id.recy_M_name);
            spec=(TextView) itemView.findViewById(R.id.recy_M_spec);
            unit = (TextView) itemView.findViewById(R.id.recy_M_unit);
        }
    }

}